"""
abort() 将屏障设置为broken状态
reset() 修复屏障，开始拦截
"""

import threading
import logging

FORMAT = "%(thread)d %(threadName)s %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)


def worker(barrier: threading.Barrier):
    logging.info('n_waiting is {}'.format(barrier.n_waiting))
    try:
        bid = barrier.wait()
        logging.info('after barrier is {}'.format(bid))
    except threading.BrokenBarrierError:
        logging.info('broken barrier is {}'.format(threading.current_thread().name))


barrier = threading.Barrier(3)

for i in range(9):
    if i == 2:
        barrier.abort()
    elif i == 6:
        barrier.reset()
    threading.Event().wait(1)
    threading.Thread(target=worker, args=(barrier,), name='worker thread is {}'.format(i)).start()
